import { HttpRequestOptions, HttpRequestBase } from "./httpRequest";
import { ElMessage } from 'element-plus';
import router from '@/router';

import { defaultHttpRequest } from "./defaultHttpRequest";
import type { Token } from "./type";

// Http请求
class AuthorizationHttpRequest extends HttpRequestBase {
    constructor() {
        const options = new HttpRequestOptions();
        const config = options.getConfig();
        config.baseURL = import.meta.env.VITE_APP_BASE_API;
        config.language = {
            defaultLanguage: "zh-cn"
        };
        config.errorAlert = (error: string) => {
            ElMessage({
                showClose: true,
                message: error,
                type: 'error'
            });
        };
        config.authorization = {
            maxRefreshTokenFrequency: 3,
            refreshToken: async (refreshToken: string) => {
                const token = await defaultHttpRequest.post<Token>('/api/basics/accounts/refresh', {
                    refreshToken
                });
                return token;
            },
            routerPush: () => {
                router.push("/login");
            }
        };
        super(config);
    }
}
export const authorizationHttpRequest = new AuthorizationHttpRequest();